package com.lzz.service.imple;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzz.dao.UserDao;
import com.lzz.entity.User;
import com.lzz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author zhenzhen
 * @date 2018-12-21 上午11:01
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public int addUser(User user) {
        String salt = UUID.randomUUID().toString().replace("-", "");
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt);
        log.info("salt:" + salt);
        log.info("md5Hash:" + md5Hash.toString());

        user.setPassword(md5Hash.toString());
        user.setSalt(salt);
        user.setState((byte) 1);
        int res = userDao.insert(user);
        return res;
    }

    @Override
    public int deleteUserById(String uuid) {
        return userDao.delete(uuid);
    }

    @Override
    public int updateUser(User user) {
        return userDao.insert(user);
    }

    @Override
    public User getUserById(String uuid) {
        return userDao.selectById(uuid);
    }

    @Override
    public User getUserByUserName(String username) {
        return userDao.selectByUserName(username);
    }

    @Override
    public PageInfo<User> getUserForPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userDao.selectPage();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }
}
